using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;

using Common = ScenarioFramework.WorkflowEngine.DataModel.Common;
using WorkflowParticipants = ScenarioFramework.WorkflowEngine.DataModel.WorkflowParticipantsData;

namespace Scenario.Modeler
{
	/// <summary>
	/// Descrizione di riepilogo per FormGrants.
	/// </summary>
	public class FormGrants : System.Windows.Forms.Form
	{
		WorkflowParticipants.XMLRoles xmlRoles;
		private Common.XMLGrants xmlGrants;

		private System.Windows.Forms.Label label1;
		private System.Windows.Forms.ComboBox cmbGrantType;
		private System.Windows.Forms.ListView listGrants;
		private System.Windows.Forms.Button btnAddRemove;
		private System.Windows.Forms.Panel panel2;
		private System.Windows.Forms.Button btnCancel;
		private System.Windows.Forms.Button btnSave;
		/// <summary>
		/// Variabile di progettazione necessaria.
		/// </summary>
		private System.ComponentModel.Container components = null;

		public FormGrants(Common.XMLGrants vGrants, ref WorkflowParticipants.XMLRoles vRoles)
		{
			//
			// Necessario per il supporto di Progettazione Windows Form
			//
			InitializeComponent();

			this.cmbGrantType.Items.Add ( Common.XMLGrant.EXECUTION_TYPE);
			this.cmbGrantType.Items.Add (Common.XMLGrant.VISIBLE_TYPE);
			this.cmbGrantType.SelectedIndex = this.cmbGrantType.Items.Add ("ALL");

			//
			// TODO: aggiungere il codice del costruttore dopo la chiamata a InitializeComponent
			//
			xmlGrants = vGrants;
			xmlRoles = vRoles;

			this.FillListGrants();
		}

		public Common.XMLGrants XMLGrants
		{
			get 
			{
				return xmlGrants;
			}
		}


		private void FillListGrants()
		{
			listGrants.Clear();
			listGrants.Columns.Add("Grant",listGrants.Width - 100, HorizontalAlignment.Left);
			listGrants.Columns.Add("Role",100, HorizontalAlignment.Left);

			for (int i=0; i<xmlGrants.Count; i++)
			{
				Common.XMLGrant grant = (Common.XMLGrant)xmlGrants[i];

				if ( (  this.cmbGrantType.Text == "ALL" ) ||
					( grant.Type == this.cmbGrantType.Text ) )
				{
					WorkflowParticipants.XMLRole xmlRole = null;
					for ( int j=0; j<grant.XMLRoles.Count; j++)
					{
						xmlRole = (WorkflowParticipants.XMLRole)grant.XMLRoles[j];
						
						ListViewItem item =null;
						item = new ListViewItem( new string[] { grant.Type, xmlRole.Key});
						listGrants.Items.Add (item);

					}
				}
			}
		}

		/// <summary>
		/// Pulire le risorse in uso.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Codice generato da Progettazione Windows Form
		/// <summary>
		/// Metodo necessario per il supporto della finestra di progettazione. Non modificare
		/// il contenuto del metodo con l'editor di codice.
		/// </summary>
		private void InitializeComponent()
		{
			this.label1 = new System.Windows.Forms.Label();
			this.cmbGrantType = new System.Windows.Forms.ComboBox();
			this.listGrants = new System.Windows.Forms.ListView();
			this.btnAddRemove = new System.Windows.Forms.Button();
			this.panel2 = new System.Windows.Forms.Panel();
			this.btnCancel = new System.Windows.Forms.Button();
			this.btnSave = new System.Windows.Forms.Button();
			this.panel2.SuspendLayout();
			this.SuspendLayout();
			// 
			// label1
			// 
			this.label1.Location = new System.Drawing.Point(8, 16);
			this.label1.Name = "label1";
			this.label1.Size = new System.Drawing.Size(480, 24);
			this.label1.TabIndex = 0;
			this.label1.Text = "Grant Type";
			// 
			// cmbGrantType
			// 
			this.cmbGrantType.Location = new System.Drawing.Point(8, 32);
			this.cmbGrantType.Name = "cmbGrantType";
			this.cmbGrantType.Size = new System.Drawing.Size(488, 24);
			this.cmbGrantType.TabIndex = 1;
			this.cmbGrantType.Text = "comboBox1";
			this.cmbGrantType.SelectedIndexChanged += new System.EventHandler(this.cmbGrantType_SelectedIndexChanged);
			// 
			// listGrants
			// 
			this.listGrants.Location = new System.Drawing.Point(8, 72);
			this.listGrants.Name = "listGrants";
			this.listGrants.Size = new System.Drawing.Size(704, 512);
			this.listGrants.Sorting = System.Windows.Forms.SortOrder.Ascending;
			this.listGrants.TabIndex = 7;
			this.listGrants.View = System.Windows.Forms.View.Details;
			// 
			// btnAddRemove
			// 
			this.btnAddRemove.Location = new System.Drawing.Point(504, 24);
			this.btnAddRemove.Name = "btnAddRemove";
			this.btnAddRemove.Size = new System.Drawing.Size(208, 32);
			this.btnAddRemove.TabIndex = 8;
			this.btnAddRemove.Text = "Add/Remove";
			this.btnAddRemove.Click += new System.EventHandler(this.btnAddRemove_Click);
			// 
			// panel2
			// 
			this.panel2.Controls.Add(this.btnCancel);
			this.panel2.Controls.Add(this.btnSave);
			this.panel2.Dock = System.Windows.Forms.DockStyle.Bottom;
			this.panel2.Location = new System.Drawing.Point(0, 603);
			this.panel2.Name = "panel2";
			this.panel2.Size = new System.Drawing.Size(720, 48);
			this.panel2.TabIndex = 15;
			// 
			// btnCancel
			// 
			this.btnCancel.Location = new System.Drawing.Point(328, 8);
			this.btnCancel.Name = "btnCancel";
			this.btnCancel.Size = new System.Drawing.Size(72, 40);
			this.btnCancel.TabIndex = 10;
			this.btnCancel.Text = "Cancel";
			this.btnCancel.Click += new System.EventHandler(this.btnCancel_Click);
			// 
			// btnSave
			// 
			this.btnSave.Location = new System.Drawing.Point(216, 8);
			this.btnSave.Name = "btnSave";
			this.btnSave.Size = new System.Drawing.Size(80, 40);
			this.btnSave.TabIndex = 9;
			this.btnSave.Text = "Save";
			this.btnSave.Click += new System.EventHandler(this.btnSave_Click);
			// 
			// FormGrants
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(6, 15);
			this.ClientSize = new System.Drawing.Size(720, 651);
			this.Controls.Add(this.panel2);
			this.Controls.Add(this.btnAddRemove);
			this.Controls.Add(this.listGrants);
			this.Controls.Add(this.cmbGrantType);
			this.Controls.Add(this.label1);
			this.Name = "FormGrants";
			this.Text = "Grants";
			this.panel2.ResumeLayout(false);
			this.ResumeLayout(false);

		}
		#endregion

		private void btnAddRemove_Click(object sender, System.EventArgs e)
		{
			Common.XMLGrant grant = null;
			try
			{
				grant = xmlGrants.GetGrant(this.cmbGrantType.Text );
			}
			catch ( ScenarioFramework.WorkflowEngine.DataModel.Exception.GrantNotFound )
			{
				grant =new Common.XMLGrant();
				grant.Type = this.cmbGrantType.Text;
				grant.Enabled = Common.XMLGrant.YES_ENABLED;
			}
				
			Participants frmParticipants = new Participants (xmlRoles, grant.XMLRoles);
			frmParticipants.ShowDialog (this);

			if ( frmParticipants.XMLRoles != null )
			{
				this.xmlRoles = frmParticipants.XMLRoles;
			}

			if ( frmParticipants.XMLRolesSelected  != null )
			{
				Common.XMLGrant grant2Change = null;
				grant.XMLRoles = frmParticipants.XMLRolesSelected;
				bool bNotFound=true;
			
				for (int i=0; i<xmlGrants.Count;i++)
				{
					grant2Change = (Common.XMLGrant)xmlGrants[i];
					if ( grant2Change.Type  == grant.Type)
					{
						xmlGrants[i] = grant;
						bNotFound=false;
					}
				}
				if ( bNotFound )
				{
					xmlGrants.Add (grant);
				}
				this.FillListGrants ();
			}
		}

		private void btnCancel_Click(object sender, System.EventArgs e)
		{
			xmlGrants = null;
			this.Close();
		}

		private void btnSave_Click(object sender, System.EventArgs e)
		{
			this.Close();
		}

		private void cmbGrantType_SelectedIndexChanged(object sender, System.EventArgs e)
		{
			if ( this.cmbGrantType.Text == "ALL" )
			{
				this.btnAddRemove.Enabled = false;
			}
			else
				this.btnAddRemove.Enabled = true;
		}
	}
}
